﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Script.Serialization;
using System.Collections.Generic;

public class DelegateService
{
    private static DelegateService instance = null;

    private HttpResponse response;
    private JavaScriptSerializer jsSerializer;
    private ResponseShell responseShell;

	private DelegateService()
	{
        jsSerializer = new JavaScriptSerializer();
	}

    public static DelegateService getInstance()
    {
        if (instance == null)
        {
            instance = new DelegateService();
        }
        return instance;
    }

    public string delegateRequest(HttpRequest request)
    {
        String jsonResponse = "";

        if (request["action"] == null)
        {
            jsonResponse = jsSerializer.Serialize(requestError("Action not specified"));
        }
        else
        {
            switch (request["action"].ToLower())
            {
                case "login":
                    jsonResponse = login(request);
                    break;
                case "getuser":
                    //TODO: getUser
                    break;
                case "getusersonproject":
                    jsonResponse = getUsersOnProject(request);
                    break;
                case "getprojectsforuser":
                    jsonResponse = getProjectsForUser(request);
                    break;
                case "gettasksforproject":
                    jsonResponse = getTasksForProject(request);
                    break;
                case "createuser":
                    jsonResponse = createUser(request);
                    break;
                case "createproject":
                    jsonResponse = createProject(request);
                    break;
                default:
                    jsonResponse = jsSerializer.Serialize(requestError("Action not recognized"));
                    break;
            }
        }
        return jsonResponse; 
    }

    public ResponseShell requestError(String message)
    {
        response.StatusCode = 400;
        ResponseShell responseShell = new ResponseShell("Failure", message);
        return responseShell;
    }

    public HttpResponse getResponse()
    {
        return response;
    }

    public void setResponse(HttpResponse response)
    {
        this.response = response;
    }

#region switch-specific functions
    public String login(HttpRequest request)
    {
        if (request["email"] == null || request["password"] == null)
        {
            responseShell = requestError("Missing email or password");
        }
        else
        {
            String sessionKey = UserService.getInstance().doLogin(request["email"], request["password"]);
            responseShell = new ResponseShell("success", sessionKey);
        }
        return jsSerializer.Serialize(responseShell);      
    }

    public String createUser(HttpRequest request)
    {
        if (request["email"] == null || request["password"] == null || request["name"] == null)
        {
            responseShell = requestError("Missing email, name or password");
        }
        else
        {
            String sessionKey = UserService.getInstance().createUser(request["email"],
                request["password"], request["name"]);
            responseShell = new ResponseShell("success", sessionKey);
        }
        return jsSerializer.Serialize(responseShell);
    }

    public String getUsersOnProject(HttpRequest request)
    {
        if (request["projectId"] == null)
        {
            responseShell = requestError("Missing projectId");
        }
        else if (request["sessionKey"] == null || SessionService.getInstance().getUserFromSession(Convert.ToInt32(request["sessionKey"])) == null)
        {
            responseShell = requestError("Session not recognized");
        }
        else
        {
            List<User> users = ProjectService.getInstance().getUsersOnProject(Convert.ToInt32(request["projectId"]));
            responseShell = new ResponseShell("Success", users);
        }
        return jsSerializer.Serialize(responseShell);
    }

    public String getProjectsForUser(HttpRequest request)
    {
        if (request["userId"] == null)
        {
            responseShell = requestError("Missing userId");
        }
        else if (request["sessionKey"] == null || SessionService.getInstance().getUserFromSession(Convert.ToInt32(request["sessionKey"])) == null)
        {
            responseShell = requestError("Session not recognized");
        }
        else
        {
            List<Project> projects = UserService.getInstance().getProjectsForUser(Convert.ToInt32(request["userId"]));
            responseShell = new ResponseShell("Success", projects);
        }
        return jsSerializer.Serialize(responseShell);
    }

    public String getTasksForProject(HttpRequest request)
    {
        if (request["projectId"] == null)
        {
            responseShell = requestError("Missing projectId");
        }
        else if (request["sessionKey"] == null || SessionService.getInstance().getUserFromSession(Convert.ToInt32(request["sessionKey"])) == null)
        {
            responseShell = requestError("Session not recognized");
        }
        else
        {
            List<Task> tasks = ProjectService.getInstance().getTasksForProject(Convert.ToInt32(request["projectId"]));
            responseShell = new ResponseShell("Success", tasks);
        }
        return jsSerializer.Serialize(responseShell);
    }

    public String createProject(HttpRequest request)
    {
        User user = SessionService.getInstance().getUserFromSession(Convert.ToInt32(request["sessionKey"]));
        if (request["sessionKey"] == null || user == null)
        {
            responseShell = requestError("Session not recognized");
        }
        else if (request["category"] == null || request["description"] == null || request["name"] == null)
        {
            responseShell = requestError("Missing name, category or description");
        }
        else
        {
            ProjectService.getInstance().createProject(request["name"], request["category"], request["description"], user);
        }
        return jsSerializer.Serialize(responseShell);
    }

#endregion

}
